package 力扣;


import java.util.Stack;

/**
 * @author yyq
 * @create 2022-04-11 19:18
 */
public class leetcode112 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root==null) return false;
        int[] sum = new int[0];
        return preOrderSum(sum,root,targetSum);
    }

    private boolean preOrderSum(int[] sum, TreeNode root, int targetSum) {
            sum[0] = sum[0] + root.val;
            boolean flag = false;

            if(root.left==null&&root.right==null){
                if(sum[0]==targetSum) return true;
            }
            if(root.left!=null){
                flag = preOrderSum(sum,root.left,targetSum);
            }
            if(flag == true) return true;
            if(root.right!=null){
                flag = preOrderSum(sum,root.right,targetSum);
            }
            if(flag == true) return true;
            sum[0] = sum[0] - root.val;
            return false;
    }
}
